<?php
class Message extends Model {
	
	private static $table_name = 'MESSAGE';
	
	public static function getMessages($idSalle) {
		$dbConnection = PDOConnection::getInstance();
		$requete= $dbConnection->query("SET NAMES UTF8;");
		$requete=$dbConnection->prepare("	SELECT *
											FROM ".self::$table_name."
											WHERE IDSALLE =:id
											AND MES_IDMESSAGE=NULL
											ORDER BY DATEPOST ASC"
										);
		$requete->execute(array('id'=>$idSalle));
		$i=0;
		$data = array();
		while($tab = $requete->fetch(PDO::FETCH_ASSOC)) {
			$data[$i]=new Message($tab);
			$i++;
		}
		return $data;
	}
	
	public static function getMessageFromId($id) {
		$dbConnection = PDOConnection::getInstance();
		$requete= $dbConnection->query("SET NAMES UTF8;");
		$requete=$dbConnection->prepare("SELECT * FROM ".self::$table_name." WHERE IDMESSAGE=:id");
		$requete->execute(array('id' => $id));
		$tab = $requete->fetch(PDO::FETCH_ASSOC);
		return new Message($tab);
	}
	
	public function getComments() {
		$dbConnection = PDOConnection::getInstance();
		$requete= $dbConnection->query("SET NAMES UTF8;");
		$requete=$dbConnection->prepare("	SELECT *
											FROM ".self::$table_name."
											WHERE MES_IDMESSAGE=:id
											ORDER BY DATEPOST ASC"
										);
		$requete->execute(array('id'=>$this->getMessageId()));
		$i=0;
		$data = array();
		while($tab = $requete->fetch(PDO::FETCH_ASSOC)) {
			$data[$i]=new Message($tab);
			$i++;
		}
		return $data;
	}
	
	public function __construct($data) {
		parent::__construct();
		$this->values = $data;
	}
	
	public function getMessageId() { return $this->values['IDMESSAGE']; }
	public function getUserId() { return $this->values['IDELEVE']; }
	public function getSalleId() { return $this->values['IDSALLE']; }
	public function getTitle() { return $this->values['TITRE']; }
	public function getContenu() { return $this->values['CONTENU']; }
	public function getNombreLike() { return $this->values['NOMBRELIKES']; }
	public function getDatePost() { return $this->values['DATEPOST']; }
	
	
	public function getAuthor() {
		$dbConnection = PDOConnection::getInstance();
		$requete= $dbConnection->query("SET NAMES UTF8;");
		$requete=$dbConnection->prepare("	SELECT *
											FROM ELEVE
											WHERE IDELEVE=:id"
										);
		$requete->execute(array('id'=>$this->getUserId()));
		$userName = $requete->fetch(PDO::FETCH_ASSOC);
		return $userName;
	}
	
	// Retourne les 10 messages les plus aimés des 7 derniers jours
	public function get10MostLikedMessages() {
		$dbConnection = PDOConnection::getInstance();
		$requete= $dbConnection->query("SET NAMES UTF8;");
		$requete=$dbConnection->prepare("	SELECT * 
											FROM MESSAGE 
											WHERE TO_DAYS(NOW()) - 7 < TO_DAYS(MESSAGE.DATEPOST)
											ORDER BY NOMBRELIKES DESC
											LIMIT 10
										");
		$requete->execute();
		$i=0;
		$data = array();
		while($tab = $requete->fetch(PDO::FETCH_ASSOC)) {
			$data[$i]=new Message($tab);
			$i++;
		}
		return $data;
	}
	
	public function isLikedBy($iduser) {
		$dbConnection = PDOConnection::getInstance();
		$requete=$dbConnection->prepare("	SELECT IDELEVE,IDMESSAGE
											FROM AIMER
											WHERE IDELEVE=:iduser
											AND IDMESSAGE=:idmessage
										");
		$requete->execute(array('iduser'=>$iduser,'idmessage'=>$this->getMessageId()));
		if(($tab = $requete->fetch(PDO::FETCH_ASSOC))=='')
			return false;
		else 
			return true;
	}
	
	// Aimer un message *****CONDITION "A DEJA AIME" *******
	public function likePost($iduser) {
		$dbConnection = PDOConnection::getInstance();
		$requete=$dbConnection->prepare("	UPDATE MESSAGE
											SET NOMBRELIKES = (NOMBRELIKES + 1)
											WHERE IDMESSAGE = :id
										");
		$requete->execute(array('id'=>$this->getMessageId()));
		
		$requete=$dbConnection->prepare("	INSERT INTO AIMER(IDELEVE,IDMESSAGE)
											VALUES(:ideleve, :idmessage)
										");
		$requete->execute(array('ideleve'=>$iduser,'idmessage'=>$this->getMessageId()));
		echo $this->getNombreLike()+1;
	}
	// Ne plus aimer un message
	public function unlikePost($iduser) {
		$dbConnection = PDOConnection::getInstance();
		$requete=$dbConnection->prepare("	UPDATE MESSAGE
											SET NOMBRELIKES = (NOMBRELIKES - 1)
											WHERE IDMESSAGE = :id
										");
		$requete->execute(array('id'=>$this->getMessageId()));
		
		$requete=$dbConnection->prepare("	DELETE FROM AIMER
											WHERE IDELEVE=:ideleve
											AND IDMESSAGE=:idmessage
										");
		$requete->execute(array('ideleve'=>$iduser,'idmessage'=>$this->getMessageId()));
		echo $this->getNombreLike()-1;
	}
	
	public static function getMyLikes($iduser) {
		$dbConnection = PDOConnection::getInstance();
		$requete= $dbConnection->query("SET NAMES UTF8;");
		$requete=$dbConnection->prepare("	SELECT * FROM ".self::$table_name." 
											LEFT JOIN AIMER
											ON MESSAGE.IDMESSAGE=AIMER.IDMESSAGE
											WHERE AIMER.IDELEVE=:id
										");
		$requete->execute(array('id'=>$iduser));
		$i=0;
		$data = array();
		while($tab = $requete->fetch(PDO::FETCH_ASSOC)) {
			$data[$i]=new Message($tab);
			$i++;
		}
		return $data;
	}
	// Créer un message.
	public static function creerMessage($idsalle,$ideleve,$titre,$contenu,$parent=NULL){
		$dbConnection = PDOConnection::getInstance();
		$requete=$dbConnection->prepare("	INSERT INTO MESSAGE(IDSALLE,IDELEVE,MES_IDMESSAGE,TITRE,CONTENU,DATEPOST)
											VALUES(:idsalle,:ideleve,:parent,:titre,:contenu,NOW())
										");
		$requete->execute(array('idsalle'=>$idsalle,'ideleve'=>$ideleve,'parent'=>$parent,'titre'=>$titre,'contenu'=>$contenu));
	}
	/*// Ajouter un commentaire à un message
	public static function addComment($idsalle,$ideleve,$titre,$contenu){
		$dbConnection = PDOConnection::getInstance();
		$requete=$dbConnection->prepare("	INSERT INTO MESSAGE(IDSALLE,IDELEVE,MES_IDMESSAGE,TITRE,CONTENU,DATEPOST)
											VALUES(:idsalle,:ideleve,:message_parent,':titre',':contenu',NOW())
										");
		$requete->execute(array('idsalle'=>$idsalle,'ideleve'=>$ideleve,'message_parent'=>$this->getMessageId(),'titre'=>$titre,'contenu'=>$contenu));
	}*/
	
	// Pour les stats
	public function getNumberOfMessages(){
		$dbConnection = PDOConnection::getInstance();
		$requete=$dbConnection->prepare("	SELECT COUNT(IDMESSAGE)
											FROM MESSAGE
										");
		$requete->execute();
		$data = $requete->fetch(PDO::FETCH_ASSOC);
		return $data['COUNT(IDMESSAGE)'];
	}
	public function getDateLastPost(){
	$dbConnection = PDOConnection::getInstance();
		$requete=$dbConnection->prepare("	SELECT DATEPOST
											FROM MESSAGE
											ORDER BY DATEPOST DESC
											LIMIT 1
										");
		$requete->execute();
		$data = $requete->fetch(PDO::FETCH_ASSOC);
		return $data['DATEPOST'];
	}
	}
